import { userStore } from '@/store/user'
import { Directive } from 'vue'

// 自定义按钮权限指令
export const permission: Directive = {
    mounted(el, binding) {
        const store = userStore()
        // value 按钮上的权限
        const { value } = binding
        // 获取用户所有的权限
        const permissions = store.getPermissions
        // 判断传递进来的按钮权限、是否存在
        if (value && value instanceof Array && value.length > 0) {
            const permissionRoles = value
            const hasPermission = permissions.some((role: any) => {
                return permissionRoles.includes(role)
            })
            if (!hasPermission) {
                // 没有权限时，隐藏
                el.style.display = 'none'
            }
        } else {
            throw new Error("need roles! Like v-permission=\"['add','edit']\"")
        }
    }
}
